package cn.hls.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.hls.dao.ACLDao;
import cn.hls.dao.PersonDao;
import cn.hls.dao.UserDao;
import cn.hls.entity.Person;
import cn.hls.entity.User;
import cn.hls.entity.vo.PageInfo;
import cn.hls.exception.BussinessException;
import cn.hls.service.PersonService;
import cn.hlsconstant.DataAssessEnum;

@Service("personService")
public class PersonServiceBean extends BaseServiceBean<Person> implements
		PersonService {
	@Resource(name = "personDaoImpl")
	private PersonDao personDao;
	@Resource(name = "userDaoImpl")
	private UserDao userDao;
	@Resource(name = "aclDaoImpl")
	private ACLDao aclDao;

	@Override
	public boolean add(Person entity) throws Exception {
		try {
			User user = entity.getUser();
			user.setId(userDao.getCustomerId());
			userDao.add(user);
			entity.setUser(user);
			entity.setId(personDao.getCustomerId());
			personDao.add(entity);
			user.setPerson(entity);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw new BussinessException(DataAssessEnum.INSERT_ERROR,"新增人员信息失败");
		}
	}
	@Override
	public boolean update(Person entity) throws Exception {
		Person tempEntity=personDao.find(entity.getId());
		tempEntity.setAddress(entity.getAddress().trim());
		tempEntity.setName(entity.getName().trim());
		tempEntity.setDuty(entity.getDuty().trim());
		tempEntity.setDutyNo(entity.getDutyNo().trim());
		tempEntity.setEmail(entity.getEmail().trim());
		tempEntity.setGender(entity.getGender());
		tempEntity.setPhone(entity.getPhone().trim());
		tempEntity.setUpdateTime(new Date());
		return super.update(tempEntity);
	}

	@Override
	public PageInfo<Person> loadPersonInfo(Person entity, PageInfo<Person> page)
			throws Exception {
		return personDao.loadPersonInfo(entity, page);
	}

	@Override
	public boolean deletePerson(Integer id) throws Exception {
		try {
			personDao.delete(id);
			// 删除该用户所拥有的权限信息
			aclDao.delete(id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw new BussinessException(DataAssessEnum.DELETE_ERROR,"删除人员信息失败");
		}
	}

}
